require('./index.scss');
var Tool = require('common/tool');
var subMenuList = require('../subMenuList');
var uploadPreview = require('common/uploadPreview');
require('comp/tab');
require('comp/dialog');
require('../../../components/enableAccount');
var uploadQueueList=[];

var vm = avalon.define({
    $id: 'uploadDeposit',
    tabList:{
        active:3,
        list:subMenuList
    },
    actionUrl:'/api/uploadDepositFileSubmit.do',                       // 上传地址
    loading:false,                      //上传中
    uploadList:"",
    actionStatus:"show",                //show 显示状态, upload 点击上传按钮后的状态
    /* 点击上传按钮 */
    uploadFn: function(){
        console.log('this.actionStatus', this.actionStatus);
        if( this.actionStatus == 'show' ){
            this.actionStatus = 'upload';
            return false;
        }

        // 验证
        var upload_ready = true;
        if( uploadQueueList.length===0 ){
            layer.alert('请先选择文件，再上传！');
            return false;
        }
        $(".uploadTb tbody tr").each(function(i,tr){
            var $tr = $(tr),
                $uploadItem = $tr.find('.uploadItem'),
                uploadText = $tr.find('.uploadFileText').text(),
                firstFile = $uploadItem.first().find('input').val(),
                lastFile = $uploadItem.last().find('input').val();
            // 同证明文件，正反面必须同时上传
            if( (firstFile=='' || lastFile=='') && firstFile+lastFile !='' ){
                layer.alert('请上传'+uploadText+'的完整信息');
                upload_ready = false;
                return false;
            }
        });

        // 提交
        if(upload_ready) ajaxFileUpload();
        return false;
    }
});


vm.$watch('onReady',function(){
    new Files();
});


function Files(){
    this.queryUrl = '/uploadFile';
    this.queryInfo();
}

Files.prototype.queryInfo = function(){
    var that = this;
    Tool.ajax({
        url: this.queryUrl,
        type:"post"
    }).done(function(res){

        var data = res.data,
            custFileMap = data.custFileMap,
            uploadList = [];
        
        var uploadDepositArr = [1,2,3]; //固定三条数据
        var textSort = ['一','二','三'];     //定义银行卡序号，中文,从1开始
        uploadDepositArr.forEach(function(v,i){
            var prov = {};
            prov.text = "存款证明"+ textSort[i];
            prov.status = custFileMap['FILE_TYPE_DEPOSIT_'+v].status;
            prov.reason = custFileMap['FILE_TYPE_DEPOSIT_'+v].reason;
            prov.paramName = "filesDepositFiles["+i+"]";
            uploadList.push(prov)
        });

        // 组装接口需要的数据
        vm.uploadList = uploadList;

        setTimeout(function(){
            that.previewImg();
        },0);
    });
}

/* 预览图片 */
Files.prototype.previewImg = function(){
    console.log($(".uploadTb .upload-btn").length);
    $(".uploadTb .upload-btn").each(function(index, el){
        var $this = $(this),
            $fileInput = $this.find('input'),
            $uploadImgBox = $this.parent().find('.uploadImgBox'),
            $uploadImg = $this.parent().find('.uploadImg');
        $fileInput.attr('id', "uploadBtn"+index);
        $uploadImgBox.attr('id', "uploadImgBox"+index);
        $uploadImg.attr('id', "uploadImg"+index);
        new uploadPreview({ 
            UpBtn: "uploadBtn"+index, 
            DivShow: "uploadImgBox"+index, 
            ImgShow: "uploadImg"+index 
        });
    });
    $("#uploadForm .uploadFileBtn").on('change', function(){
        uploadQueueList.push( $(this).val() );
    });

}

/* 上传 */
function ajaxFileUpload(){
    vm.loading = true;
    var fileElementIds = [];    
    $(".uploadFileBtn").each(function(){
        fileElementIds.push($(this).attr('id'));
    });
    $.ajaxFileUpload({
        url : vm.actionUrl, //请求url  
        secureuri : false, // 是否需要安全协议，一般设置为false
        fileElementId : fileElementIds, // 文件上传域的ID
        dataType : 'text', // 返回值类型
        data : {}, // 参数
        success : function(res){ // 服务器成功响应处理函数
            var res = eval("("+res+")");
            var msg = res.code=='0' ? '上传成功！' : res.msg;
            layer.alert(msg,function(){
                location.reload();
            });
            vm.loading = false;
        },
        error : function(){// 服务器响应失败处理函数
            vm.loading = false;
            layer.alert('上传失败');
        }
    })
}